import 'dart:convert' show json;

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class MahjongUserSwapCard {
  MahjongUserSwapCard({
    this.inSwapCard,
    this.outSwapCard,
  });

  factory MahjongUserSwapCard.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : MahjongUserSwapCard(
              inSwapCard: SwapCard.fromJson(
                  asT<Map<String, dynamic>>(jsonRes['inSwapCard'])),
              outSwapCard: SwapCard.fromJson(
                  asT<Map<String, dynamic>>(jsonRes['outSwapCard'])),
            );

  SwapCard inSwapCard;
  SwapCard outSwapCard;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'inSwapCard': inSwapCard,
        'outSwapCard': outSwapCard,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class SwapCard {
  SwapCard({
    this.userId,
    this.cards,
  });

  factory SwapCard.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }
    final List<int> cards = jsonRes['cards'] is List ? <int>[] : null;
    if (cards != null) {
      for (final dynamic item in jsonRes['cards']) {
        if (item != null) {
          cards.add(asT<int>(item));
        }
      }
    }

    return SwapCard(
      userId: asT<String>(jsonRes['userId']),
      cards: cards,
    );
  }

  String userId;
  List<int> cards;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'userId': userId,
        'cards': cards,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
